#include "Ddx_game.h"
#include "SaveFilePath.h"


#ifdef __cplusplus
extern "C" {
#endif

char  SaveFilePath[256];
BOOL g_IsCanSave;
extern void   ChangePath(char *pDesFilePath,char  *pFileName);


BOOL GetSavePath(HINSTANCE hInstance ,LPWSTR lpCmdLine)
{
#ifdef _C_SYS_MSTAR
	char TempFile[256];
	char*	GameName = GAME_NAME;
	WIN32_FIND_DATA FindFileData;
	SECURITY_ATTRIBUTES SecurityAttributes;
	int i = 0;
//	LogPrintf("===================1\n");
	if(*lpCmdLine == NULL)
	{
		WCHAR strFolder[256];
		GetModuleFileName((HMODULE)hInstance, strFolder, 256 );
		UnicodeToChar((UCHAR*)SaveFilePath, strFolder, lage_En);
		char *ptr = strrchr_Ex(SaveFilePath, '\\');
		if (ptr)
		{
			*(ptr+1) = 0;
		}
		else
		{
			printf((char*)"The file Path is wrong \n ");
		}
		printf("save path1 = %s\n",SaveFilePath);
		g_IsCanSave = FALSE;
		return FALSE;
	}
	else
		{
			g_IsCanSave = TRUE;
			UnicodeToChar((UCHAR*)SaveFilePath, lpCmdLine, lage_En);
			printf("save path2 = %s\n",SaveFilePath);
		}
//	printf("=======================2\n");
	for(;i<256;i++)
	{
		TempFile[i] = '\0';
		if((SaveFilePath[i] == '\\')||(SaveFilePath[i] == '\0'))
		{
			if(SaveFilePath[i] == '\0')
			{
				TCHAR Temp[256];
				GetUnicodeString((LPTSTR)Temp, (LPCSTR)SaveFilePath, 0 );
				HANDLE FileHandle = FindFirstFile((LPCTSTR)Temp, &FindFileData);
				if(FileHandle == INVALID_HANDLE_VALUE)
				{
					if(CreateDirectory((LPCTSTR)Temp,&SecurityAttributes))
						printf("%s\n",Temp);
				}
				else 
					FindClose(FileHandle);
				if(SaveFilePath[i-1] != '\\')
				{
					TempFile[i] = '\\';
					SaveFilePath[i] = '\\';
					SaveFilePath[i+1] = '\0';
				}
				else
					{
						SaveFilePath[i] = '\0';
					}
				break;
			}
			if(i>0)
			{
				TCHAR Temp[256];
				GetUnicodeString((LPTSTR)Temp, (LPCSTR)TempFile, 0 );
				HANDLE FileHandle = FindFirstFile((LPCTSTR)Temp, &FindFileData);
				if(FileHandle == INVALID_HANDLE_VALUE)
				{
					if(CreateDirectory((LPCTSTR)Temp,&SecurityAttributes))
						printf("%s\n",Temp);
				}else
					FindClose(FileHandle);

			}
		}
		TempFile[i] = SaveFilePath[i];
		
	}
	size_t a =0;
	printf("%s\n%s=========1\n",SaveFilePath,GameName);
	for(;a<strlen_Ex(GameName);)
	{
//		printf("@#%d\n",GameName[a]);
		if(SaveFilePath[i+a] == '\0')
		{
			SaveFilePath[i+a] = GameName[a];
			++a;
			SaveFilePath[i+a] = '\0';
		}
		else
		{
			SaveFilePath[i+a+1] = GameName[a];
			++a;
			SaveFilePath[i+a+1] = '\0';
		}
	}
	printf("%s,%s=======\n",SaveFilePath,GameName);
	TCHAR Temp[256];
	GetUnicodeString((LPTSTR)Temp, (LPCSTR)SaveFilePath, 0 );
	HANDLE FileHandle = FindFirstFile((LPCTSTR)Temp, &FindFileData);
	if(FileHandle == INVALID_HANDLE_VALUE)
	{
		if(CreateDirectory((LPCTSTR)Temp,&SecurityAttributes))
			printf("%s\n",Temp);
	}else
		FindClose(FileHandle);
	if(SaveFilePath[i+a] == '\0')
	{
		SaveFilePath[i+a] = '\\';
		SaveFilePath[i+a+1] = '\0';
	}
	else if(SaveFilePath[i+a+1] == '\0')
	{
		SaveFilePath[i+a+1] = '\\';
		SaveFilePath[i+a+2] = '\0';
	}
	printf("%s\n%s=========2\n",SaveFilePath,GameName);
	return TRUE;
#else
	WCHAR strFolder[256];
	GetModuleFileName((HMODULE)hInstance, strFolder, 256 );
	UnicodeToChar((UCHAR*)SaveFilePath, strFolder, lage_En);
	char *ptr = strrchr_Ex(SaveFilePath, '\\');
	if (ptr)
	{
		*(ptr+1) = 0;
	}
	else
	{
		printf((char*)"The file Path is wrong \n ");
	}
	g_IsCanSave = TRUE;
	return TRUE;
#endif
}


void	ChangeSavePath(char *FilePath, char *FileName)
{
	if(!g_IsCanSave)
	{
		ChangePath(FilePath, FileName);
		return;
	}
	int i = 0;
	int a = 0;
	char TempPath[256];
	memcpy_Ex(TempPath,SaveFilePath,256*sizeof(char));
	while(TempPath[i] != '\0')
	{
		if(TempPath[i+1] == '\0')
		{
			if(FileName[a] != '\0')
			{
				TempPath[i+1] = FileName[a];
				TempPath[i+2] = '\0';
				a++;
			}else
				break;
		}
		i++;
	}
	//GetUnicodeString((LPTSTR) FilePath, (LPCSTR)TempPath,  0);
	memcpy_Ex(FilePath,TempPath,256*sizeof(char));
//	printf("==========%s==========\n",TempPath);
}

#ifdef __cplusplus
}
#endif
